<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 分糖果（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>小明从糖果盒中随意抓一把糖果&#xff0c;每次小明会取出一半的糖果分给同学们。</p> 
<p>当糖果不能平均分配时&#xff0c;小明可以选择从糖果盒中&#xff08;假设盒中糖果足够&#xff09;取出一个糖果或放回一个糖果。</p> 
<p>小明最少需要多少次&#xff08;取出、放回和平均分配均记一次&#xff09;&#xff0c;能将手中糖果分至只剩一颗。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>抓取的糖果数&#xff08;&lt;10000000000&#xff09;&#xff1a;15</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>最少分至一颗糖果的次数&#xff1a;5</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:88px;">输入</td><td style="width:410px;">15</td></tr><tr><td style="width:88px;">输出</td><td style="width:410px;">5</td></tr><tr><td style="width:88px;">说明</td><td style="width:410px;"> 
    <ol><li>15&#43;1&#61;16;</li><li>16/2&#61;8;</li><li>8/2&#61;4;</li><li>4/2&#61;2;</li><li>2/2&#61;1;</li></ol></td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E5%88%86%E6%9E%90">题目分析</h4> 
<p>本题由于是每次折半&#xff0c;因此本题数量级即便很大&#xff0c;也不怕超时。</p> 
<p></p> 
<p>没有了超时的后顾之忧&#xff0c;本题&#xff0c;直接可以暴力逻辑求解&#xff0c;假设输入的是num&#xff0c;分配次数count初始为0&#xff0c;那么&#xff1a;</p> 
<ul><li>如果num % 2 &#61;&#61; 0&#xff0c;则可以直接折半&#xff0c;此时分配次数count&#43;&#43;&#xff0c; num /&#61; 2</li><li>如果num % 2 !&#61;0&#xff0c;则不可以直接折半&#xff0c;此时需要开两个分支&#xff1a;</li></ul> 
<ol><li>取出一个糖&#xff0c;即num &#43;&#61; 1&#xff0c;然后分配次数count&#43;&#43;&#xff0c;之后继续前面折半逻辑</li><li>放回一个糖&#xff0c;即num -&#61; 1&#xff0c;然后分配次数count&#43;&#43;&#xff0c;之后继续前面折半逻辑</li></ol> 
<p>最终我们只需要在众多分支中&#xff0c;取最少的count即可。</p> 
<p></p> 
<p>上面逻辑可以基于递归实现。具体实现请看代码。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    System.out.println(getResult(sc.nextLong()));
  }

  public static long getResult(long num) {
    int[] ans &#61; {Integer.MAX_VALUE};
    recursive(num, 0, ans);
    return ans[0];
  }

  public static void recursive(long num, int count, int[] ans) {
    if (num &#61;&#61; 1) {
      ans[0] &#61; Math.min(ans[0], count);
      return;
    }

    if (num % 2 &#61;&#61; 0) {
      recursive(num / 2, count &#43; 1, ans);
    } else {
      recursive(num &#43; 1, count &#43; 1, ans);
      recursive(num - 1, count &#43; 1, ans);
    }
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法实现</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  console.log(getResult(Number(line)));
});

function getResult(num) {
  ans &#61; [Infinity];
  recursive(num, 0, ans);
  return ans[0];
}

function recursive(num, count, ans) {
  if (num &#61;&#61; 1) {
    ans[0] &#61; Math.min(ans[0], count);
    return;
  }

  if (num % 2 &#61;&#61; 0) {
    recursive(num / 2, count &#43; 1, ans);
  } else {
    recursive(num &#43; 1, count &#43; 1, ans);
    recursive(num - 1, count &#43; 1, ans);
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python">import sys

# 输入获取
num &#61; int(input())


def recursive(num, count, ans):
    if num &#61;&#61; 1:
        ans[0] &#61; min(ans[0], count)
        return

    if num % 2 &#61;&#61; 0:
        recursive(num // 2, count &#43; 1, ans)
    else:
        recursive(num &#43; 1, count &#43; 1, ans)
        recursive(num - 1, count &#43; 1, ans)


# 算法入口
def getResult():
    ans &#61; [sys.maxsize]
    recursive(num, 0, ans)
    return ans[0]


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;limits.h&gt;

#define MIN(a,b) (a) &lt; (b) ? (a) : (b)

void recursive(long long num, int count);

int ans &#61; INT_MAX;

int main() {
    long long num;
    scanf(&#34;%lld&#34;, &amp;num);

    recursive(num, 0);

    printf(&#34;%d\n&#34;, ans);

    return 0;
}

void recursive(long long num, int count) {
    if(num &#61;&#61; 1) {
        ans &#61; MIN(ans, count);
        return;
    }

    if(num % 2 &#61;&#61; 0) {
        recursive(num / 2, count &#43; 1);
    } else {
        recursive(num &#43; 1, count &#43; 1);
        recursive(num - 1, count &#43; 1);
    }
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>